/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.config.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigRegisterCaseFileMapper;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigRegisterCaseMapper;
import com.icinfo.cloud.provider.punish.config.model.ConfigRegisterCase;
import com.icinfo.cloud.provider.punish.config.model.ConfigRegisterCaseFile;
import com.icinfo.cloud.provider.punish.config.service.IConfigRegisterCaseFileService;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 描述: 立案材料配置表 config_register_case_file 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年11月30日
 */
@Service
public class ConfigRegisterCaseFileServiceImpl extends MyBatisServiceSupport implements IConfigRegisterCaseFileService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ConfigRegisterCaseFileServiceImpl.class);

    @Resource
    private ConfigRegisterCaseFileMapper configRegisterCaseFileMapper;
    @Resource
    private ConfigRegisterCaseMapper configRegisterCaseMapper;

    @Override
    public void saveList(List<ConfigRegisterCaseFile> fileList) {
        Optional<ConfigRegisterCaseFile> any = fileList.stream().filter(f -> StringUtils.isAnyBlank(f.getRelId(), f.getTypeCode())).findAny();
        if (any.isPresent()){
            throw new BusinessException("立案材料配置附件参数缺失");
        }
        for (ConfigRegisterCaseFile file : fileList) {
            if (StringUtils.isBlank(file.getId())){
                file.setId(StringUtils.uuid());
                configRegisterCaseFileMapper.insertSelective(file);
            }else {
                configRegisterCaseFileMapper.updateByPrimaryKeySelective(file);
            }
        }
    }

    @Override
    public void delete(String id) {
        ConfigRegisterCaseFile file = new ConfigRegisterCaseFile();
        file.setId(id);
        file.setIsDelete(Constant.STRING_1);
        configRegisterCaseFileMapper.updateByPrimaryKeySelective(file);
    }

    /**
     * 立案材料配置列表-配置参数-获取材料列表
     * @param relId
     * @return
     */
    @Override
    public List<ConfigRegisterCaseFile> getFileListByRelId(String relId) {
        if (StringUtils.isBlank(relId)){
            return new ArrayList<>();
        }
        Example example = new Example(ConfigRegisterCaseFile.class);
        example.createCriteria()
                .andIn("relId",Arrays.asList(relId))
                .andEqualTo("isDelete",Constant.STRING_0);
        example.orderBy("sort").asc();
        return configRegisterCaseFileMapper.selectByExample(example);
    }

    /**
     * 根据处罚事项获取材料列表
     * @param paramNumbers 编码规则：机构code+处罚code
     * @return
     */
    @Override
    public List<ConfigRegisterCaseFile> getFileListByParamNumber(List<String> paramNumbers) {
        if (CollectionUtils.isEmpty(paramNumbers) || paramNumbers.size() == 0){
            return new ArrayList<>();
        }
        UserDTO userInfo = UserUtil.getUserInfo();
        if (ObjectUtil.isEmpty(userInfo)){
            throw new BusinessException("用户机构获取失败");
        }
        String id;
        List<String> ids = new ArrayList<>();
        for (String paramNumber : paramNumbers) {
            id = configRegisterCaseMapper.getConfigIdByParamNumber(paramNumber,userInfo.getOrgCode(),Constant.STRING_2);
            if (StringUtils.isBlank(id)){
                id = configRegisterCaseMapper.getConfigIdByParamNumber(paramNumber,userInfo.getOrgCode(),Constant.STRING_1);
            }
            if (StringUtils.isNotBlank(id)){
                ids.add(id);
            }
        }
        if (CollectionUtils.isNotEmpty(ids) && ids.size() > 0){
            return configRegisterCaseMapper.getFileListByIds(ids);
        }
        return new ArrayList<>();
    }
}
